/*
 * @Author: yxiawen
 * @Date: 2021-09-02 14:20:40
 * @LastEditTime: 2022-01-17 21:36:32
 * @LastEditors: yxiawen
 * @Description: 请求配置
 * @FilePath: /vite-project/src/config/requestConfig.js
 */

// import store from "@/store";
import axios from "axios";
// import router from "@/router";
import { message } from "ant-design-vue";

// http request 拦截器
axios.interceptors.request.use(
  config => {
    // const token = store.getters.getToken;
    // const url = config.url.substring(config.url.lastIndexOf("/") + 1, config.url.length);
    // if (url !== "doLogin") {
    //   if (token) {
    //     config.headers.token = `${token}`;
    //   }
    // }
    // if (config.url.indexOf("export") > 0 || config.url.indexOf("storage") > 0) {
    //   config.responseType = "blob";
    // }
    return config;
  },
  err => {
    return Promise.reject(err);
  }
);

// http response 拦截器
axios.interceptors.response.use(
  response => {
    // if (response.data.code === 204) store.dispatch("reLogin");
    return response.data;
  },
  error => {
    if (error.response) {
      // const loginPath = process.env.NODE_ENV === "production" ? "/login" : "/login";
      message.error(`${error.response.data.msg ? error.response.data.msg : error.response.status + ":服务异常，请联系系统管理员"}`);
      switch (error.response.status) {
        case 204:
        // store.dispatch("reLogin");
      }
      // window.reload();
    }
    // console.log(JSON.stringify(error));//console : Error: Request failed with status code 402
    return Promise.reject(error.response.data);
  }
);

export default axios;
